'use strict';



class ServerDao {
    handle: any
    constructor(db) {
        this.handle = db.collection("server")
        this.handle.createIndex("serverID", 1)
        this.handle.createIndex("newServerID", 1)
    }

    newAndSave = async (serverID, name, time): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            active: false,
            name: name || '',
            serverID: serverID,
            newServerID: serverID, //合并到那个服务器上,
            time: time || date,
        });
    }

    findOne = async (query): Promise<any> => {
        return await this.handle.findOne(query)
    }

    find = async (query): Promise<any[]> => {
        return await this.handle.find(query).toArray()
    }

    findByPage = async (query, start: number, size: number): Promise<any[]> => {
        return await this.handle.find(query).skip(start).limit(size).toArray()
    }

    findByPageAndSort = async (query, sort, start, size): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).skip(start).limit(size).toArray()
    }
    //opts  { multi: true}
    updateMany = async (query, data): Promise<any> => {
        return await this.handle.updateMany(query, data);
    }

    updateOne = async (query, data): Promise<any> => {
        return await this.handle.updateOne(query, data);
    }

    count = async (query): Promise<number> => {
        return await this.handle.countDocuments(query)
    }

    remove = async (query): Promise<any> => {
        return await this.handle.remove(query, {
            multi: true
        });
    }
}

export default ServerDao